<template>
    <div class="box">
        <div style="width: 100%;height: 6rem;background: black;color: white; display: flex;
        justify-content: center;
        align-items: center;;">
            <h3>短信验证码登录</h3>
        </div>

        <van-form @submit="onSubmit">
            <van-cell-group inset>

                <van-field v-model="username" name="username" type="tel" label="手机号" placeholder="手机号"
                    :rules="[{ required: true, message: '请填手机号' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="短信验证码"
                    :rules="[{ required: true, message: '请填写密码' }]">

                    <template #button>
                        <van-button size="small" type="primary">获取验证码</van-button>
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
            </div>
        </van-form>
        <img src="../../img/2.png" alt="" style="margin-top: 28rem;">
    </div>
</template>

<script setup>
import { useCounterStore } from '@/stores/counter'
import { loginApi } from '@/api/api';
import { ref } from 'vue';
let username = ref('')
let password = ref('')
import { showToast } from 'vant';
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()


const onSubmit = () => {
    loginApi(username.value, password.value).then((res) => {
        console.log(res);
        if (res.data.code == 0) {
            showToast(res.data.msg);
            router.push('/home/index')
            // 在组件内部的任何地方均可以访问变量 `store` ✨
            const store = useCounterStore() 
            store.getters(res.data.data)
            localStorage.setItem('token',res.data.data.token)
        } else {
            showToast(res.data.msg);
        }
    })
}

</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 100%;
    background: #eee;
    .van-form {
        margin-top: 2rem;
    }
}
</style>